import { reactive, ref } from "vue"
import { shuffleArray, getAudioResourcesUrl, getAudioResourcesUrlToArray, useAudio, getWordsAudioUrl, getLetterAudioUrl } from "@/utils"
import { usePingYi } from "@/utils/words/pingyi.js"
import { pinyiqueryletterbyids } from "@/api/index"
export function useTopic(props){
	const topicState = reactive({
		options: [],
		audio: [],
		topic: {},
		letter_select_result: [],
		tone_select_result: '',
		resourcesAudio: [],
		isWhole: false
	})
	const { useGetToneToNumber } = usePingYi()
	const { useGetDownloadAudio, usePlayAudioArray } = useAudio()
	// 选择次数
	const checkNumber = ref(0)
	// 年级
	const grade = props.grade * 1 - 1
	
	/**
	 * 1：选字母
	 */
	const useSelectLetterTopicInit = (row) => {
		const ids = row.options.split('|').join(',')
		return new Promise((resolve, reject) => {
			pinyiqueryletterbyids({ids}).then(async (res) => {
				const data = res.content
				let topic = null
				data.forEach(item => {
					item.pinyin = item.letter
					item.words = item.letter
					if(item.id == row.topic) { 
						topic = item
						topicState.topic.pinyin = item.pinyin
					}
				})
				if(topic) {
					const letter = topic?.audio || topic?.letter
					const audioTone = getLetterAudioUrl([letter])
					// 获取音频
					const audioItem = await useGetDownloadAudio(audioTone)
					topicState.resourcesAudio = audioItem
					// 选项
					const newData = shuffleArray(data)
					topicState.options = newData
				} 
				resolve()
			})
		})
	}
	
	/**
	 * 2：选字母、声调
	 * result1: "sh-u-ang"
		result2: "3-4"
		topic: "爽"
		整体
		result1: "chi"
		result2: "4-3"
		topic: "翅-1"
	 */
	const useSelectLetterToneTopicInit = async (row) => {
		// 是否整体认读音节
		topicState.isWhole = row.topic.indexOf('-1') !== -1
		// 声调
		const toneSplit = row.result2.split('-')
		topicState.topic.words = row.topic.substring(0, 1);
		topicState.topic.pinyin = topicState.isWhole ? row.result1 : row.result1.split('-').join('')
		topicState.topic.tone = toneSplit[0]
		topicState.topic.position = toneSplit[1]
		const audioWords = getWordsAudioUrl([topicState.topic.words])
		// 获取音频
		const audioItem = await useGetDownloadAudio(audioWords)
		topicState.resourcesAudio = audioItem
		
	}
	const useSelectLetterToneChange = () => {
		return new Promise((resolve, reject) => {
			const letter = topicState.letter_select_result.join('')
			if(letter === topicState.topic.pinyin && topicState.topic.tone === topicState.tone_select_result) {
				resolve()
			}else{
				
			}
		})
	}
	
	
	/**
	 * 3：选拼音
	 */
	const useSelectPinyiTopicInit = async (row) => {
		const options = []
		// 格式化题目
		const formatTopic = row.result1.split(',');
		topicState.topic.tone = formatTopic[0]
		topicState.topic.words = formatTopic[1]
		topicState.topic.pinyin = row.topic
		const tones = useGetToneToNumber(formatTopic[0].split('-'))
		const audioTone = getAudioResourcesUrlToArray(tones, '/pingyi/basis/')
		const audioWords = getWordsAudioUrl([topicState.topic.words])
		// 获取音频
		const audioItem = await useGetDownloadAudio([...audioTone, ...audioWords])
		topicState.resourcesAudio = audioItem
		// 格式化选项
		row.result2.split(',').forEach(item => {
			const obj = {}
			obj.pinyin = item
			obj.words = item
			options.push(obj)
		})
		const newData = shuffleArray(options)
		topicState.options = newData
	}
	
	/**
	 * 4：选字
	 */
	const useSelectWordsTopicInit = (row) => {
		const options = []
		// 格式化题目
		const formatTopic = row.topic.split('-');
		topicState.topic.words = formatTopic[0]
		topicState.topic.pinyin = formatTopic[1]
		// 格式化选项
		row.result1.split(',').forEach(item => {
			const obj = {}
			const split = item.split('-')
			obj.words = split[0]
			obj.pinyin = split[1]
			options.push(obj)
		})
		const newData = shuffleArray(options)
		topicState.options = newData
	}
	
	
	/**
	 * 5：选词组
	 */
	const useSelectPhraseTopicInit = (row) => {
		const options = []
		// 格式化题目
		const formatTopic = row.topic.split('-');
		topicState.topic.words = formatTopic[0]
		topicState.topic.pinyin = formatTopic.slice(1).join(' ')
		// 格式化选项
		row.result1.split(',').forEach(item => {
			const obj = {}
			const split = item.split('-')
			obj.words = split[0]
			obj.pinyin = split.slice(1).join(' ')
			options.push(obj)
		})
		const newData = shuffleArray(options)
		topicState.options = newData
	}
	const useSelectPhraseChange = (option) => {
		return new Promise((resolve, reject) => {
			if(option.pinyin === topicState.topic.pinyin) {
				resolve()
			}else{
				if(checkNumber.value === 1) {
					reject(true)
					return
				}
				checkNumber.value++
				reject(false)
			}
		})
	}
	/**
	 * 播放音频
	 */
	const usePlayAudio = () => {
		
	}
	
	return {
		topicState,
		useSelectLetterTopicInit,
		useSelectLetterToneTopicInit,
		useSelectPhraseTopicInit,
		useSelectWordsTopicInit,
		useSelectPinyiTopicInit,
		useSelectPhraseChange,
		usePlayAudioArray
	}
}